﻿local _, ns = ...
--光环应用到姓名版对应的属性
local AurasEvent = CreateFrame('Frame')
AurasEvent:RegisterEvent("UNIT_AURA")
AurasEvent:RegisterEvent("NAME_PLATE_UNIT_ADDED")
AurasEvent:RegisterEvent("NAME_PLATE_UNIT_REMOVED")
AurasEvent:SetScript("OnEvent", function(self, event,frame, unitAuraUpdateInfo)
	if not string.match(frame,"nameplate") then return end
	local namePlate = C_NamePlate.GetNamePlateForUnit(frame,false)
	if not namePlate then return end
	local unitFrame = namePlate.UnitFrame
	if not unitFrame then return end
	if not unitFrame.unit then return end
	
	if event == "NAME_PLATE_UNIT_ADDED" then
		unitFrame.useHpColor = nil
		unitFrame.useNameColor = nil
		unitFrame.useBarTexture = nil
		AuraUtil.ForEachAura(unitFrame.unit, "HARMFUL",nil, function(aura)
			if aura.sourceUnit == "player" or aura.sourceUnit == "pet" or aura.sourceUnit == "vehicle" then
				if type(PlateColorDB["Auras"][aura.spellId]) == "table" then
					if PlateColorDB["Auras"][aura.spellId]["usehp"] then
						if unitFrame.useHpColor ~= aura then
							unitFrame.useHpColor = aura
						end
					end
					if PlateColorDB["Auras"][aura.spellId]["usename"] then
						if unitFrame.useNameColor ~= aura then
							unitFrame.useNameColor = aura
						end
					end
					if PlateColorDB["Auras"][aura.spellId]["usetexture"] then
						if unitFrame.useBarTexture ~= aura then
							unitFrame.useBarTexture = aura
						end
					end
				end
			end
		end,true)
		C_Timer.After(0.1,function()
			ns.UpdateHpbarColor(unitFrame)
			ns.CteatNewPlateName(unitFrame)
			ns.UpdateHpTexture(unitFrame)
		end)
	end
	
	if event ~= "UNIT_AURA" then return end
	if unitAuraUpdateInfo.addedAuras ~= nil then
		for _, aura in ipairs(unitAuraUpdateInfo.addedAuras) do
			if (aura.sourceUnit ~= "player" and aura.sourceUnit ~= "pet" and aura.sourceUnit ~= "vehicle") then return end
			if type(PlateColorDB["Auras"][aura.spellId]) == "table" then
				if PlateColorDB["Auras"][aura.spellId]["usehp"] then
					if unitFrame.useHpColor ~= aura then
						unitFrame.useHpColor = aura
						C_Timer.After(0.1,function()  
							ns.UpdateHpbarColor(unitFrame)
						end)
					end
				end
				if PlateColorDB["Auras"][aura.spellId]["usename"] then
					if unitFrame.useNameColor ~= aura then
						unitFrame.useNameColor = aura
						C_Timer.After(0.1,function()
							ns.CteatNewPlateName(unitFrame)
						end)
					end
				end
				if PlateColorDB["Auras"][aura.spellId]["usetexture"] then
					if unitFrame.useBarTexture ~= aura then
						unitFrame.useBarTexture = aura
						C_Timer.After(0.1,function()
							ns.UpdateHpTexture(unitFrame)
						end)
					end
				end
			end
		end
	end
	if unitAuraUpdateInfo.updatedAuraInstanceIDs ~= nil then
		for _, auraInstanceID in ipairs(unitAuraUpdateInfo.updatedAuraInstanceIDs) do
			if unitFrame.useHpColor and unitFrame.useHpColor.auraInstanceID == auraInstanceID then
				local newAura = C_UnitAuras.GetAuraDataByAuraInstanceID(unitFrame.unit, auraInstanceID);
				unitFrame.useHpColor = newAura
				
				C_Timer.After(0.1,function()  
					ns.UpdateHpbarColor(unitFrame)
				end)
			end
			if unitFrame.useNameColor and unitFrame.useNameColor.auraInstanceID == auraInstanceID then
				local newAura = C_UnitAuras.GetAuraDataByAuraInstanceID(unitFrame.unit, auraInstanceID);
				unitFrame.useNameColor = newAura
				C_Timer.After(0.1,function()
					ns.CteatNewPlateName(unitFrame)
				end)
			end
			if unitFrame.useBarTexture and unitFrame.useBarTexture.auraInstanceID == auraInstanceID then
				local newAura = C_UnitAuras.GetAuraDataByAuraInstanceID(unitFrame.unit, auraInstanceID);
				unitFrame.useBarTexture = newAura
				C_Timer.After(0.1,function()
					ns.UpdateHpTexture(unitFrame)
				end)
			end
		end
	end

	if unitAuraUpdateInfo.removedAuraInstanceIDs ~= nil then
		for _, auraInstanceID in ipairs(unitAuraUpdateInfo.removedAuraInstanceIDs) do
			if unitFrame.useHpColor and unitFrame.useHpColor.auraInstanceID == auraInstanceID then
				unitFrame.useHpColor = nil
				C_Timer.After(0.1,function()  
					ns.UpdateHpbarColor(unitFrame)
				end)
			end
			if unitFrame.useNameColor and unitFrame.useNameColor.auraInstanceID == auraInstanceID then
				unitFrame.useNameColor = nil
				C_Timer.After(0.1,function()
					ns.CteatNewPlateName(unitFrame)
				end)
			end
			if unitFrame.useBarTexture and unitFrame.useBarTexture.auraInstanceID == auraInstanceID then
				unitFrame.useBarTexture = nil
				C_Timer.After(0.1,function()
					ns.UpdateHpTexture(unitFrame)
				end)
			end
		end
	end
end)